package com.hanxun.controller;

import com.alibaba.fastjson.JSONObject;
import com.hanxun.component.ConfirmCallbackService;
import com.hanxun.component.ReturnCallbackService;
import com.hanxun.config.WorkExchangeQueueConfig;
import com.hanxun.model.User;
import lombok.AllArgsConstructor;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: zzq
 * @Data:2024-02-06 1:40
 */
@RestController
@AllArgsConstructor
public class TestController {

    private final RabbitTemplate rabbitTemplate;
    private final ConfirmCallbackService confirmCallbackService;
    private final ReturnCallbackService returnCallbackService;
    @GetMapping("/Work")
    public String test3(){
        for (int i = 0; i < 5; i++) {
            User user = User.builder()
                    .name("Work" + i)
                    .build();
            String msg = JSONObject.toJSONString(user);
            CorrelationData correlationData = new CorrelationData("id_" + System.currentTimeMillis() + "");
            rabbitTemplate.setConfirmCallback(confirmCallbackService);
            rabbitTemplate.setReturnCallback(returnCallbackService);
            rabbitTemplate.convertAndSend(WorkExchangeQueueConfig.TEST_WORK_QUEUE, msg, message -> {
                message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                return message;
            },correlationData);
        }
        return "———————————Work—————————Work—————————————";
    }
}
